package com.ruoyi.generator.controller;

import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.generator.domain.DatabaseTable;
import com.ruoyi.generator.domain.GenTable;
import com.ruoyi.generator.domain.GenTableColumn;
import com.ruoyi.generator.service.IGenTableColumnService;
import com.ruoyi.generator.service.IGenTableService;
import com.ruoyi.generator.service.impl.FromBuildConfigServiceImpl;
import com.ruoyi.generator.service.sqlexec.ExeDbSqlService;
import com.ruoyi.system.service.impl.SysDictTypeServiceImpl;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成 操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/tool/dbexec")
public class ExeDbSqlController{

    @Autowired
    private ExeDbSqlService exeDbSqlService;

    /**
     * 运行SQL文件
     */
    @PostMapping("/upload")
    public AjaxResult upload(@RequestParam("file") MultipartFile file){
        return exeDbSqlService.exeSqlFile(file);
    }

    /**
     * 重置系统数据
     */
    @GetMapping("/resetSysData")
    public AjaxResult resetSysData(){
        return exeDbSqlService.resetSysData();
    }


    /**
     * 获取执行任务状态
     */
    @GetMapping("/progress/{target}")
    public AjaxResult getTaskProgress(@PathVariable("target") String target){
        return AjaxResult.success(exeDbSqlService.getExecutionProgress(target));
    }

    /**
     * 删除执行任务
     */
    @GetMapping("/del/{target}")
    public AjaxResult delTask(@PathVariable("target") String target){
        return AjaxResult.success(exeDbSqlService.delExecutionTask(target));
    }

}
